<template>
  <div>
    <h2>题目详情</h2>
    <div>题目ID：{{ questionId }}</div>
    <el-table :data="submissionList" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="homeworkName" label="作业名称" />
      <el-table-column prop="userName" label="用户名" />
      <el-table-column prop="programType" label="编程语言">
        <template #default="{ row }">
          <span v-if="row.programType === 0">Java</span>
          <!-- <span v-else-if="row.programType === 2">C++</span>
          <span v-else-if="row.programType === 3">Python</span> -->
          <span v-else>其它</span>
        </template>
      </el-table-column>
      <el-table-column prop="userCode" label="学生代码">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="showCode(row)">查看代码</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="pass" label="是否通过">
        <template #default="{ row }">
          <span v-if="row.pass === 1">通过</span>
          <span v-else>未通过</span>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" title="学生代码" width="60%" :close-on-click-modal="false">
      <v-md-preview :text="codeToMarkdown(currentCode, currentLang)" style="max-width:100%" />
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getHomeworkUserDTOListService } from '@/apis/homework'
import VMdPreview from '@kangc/v-md-editor/lib/preview'
import '@kangc/v-md-editor/lib/style/preview.css'
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js'
import '@kangc/v-md-editor/lib/theme/style/github.css'
import Prism from 'prismjs'

VMdPreview.use(githubTheme, { Prism })

const route = useRoute()
const questionId = route.params.questionId
const submissionList = ref([])

const dialogVisible = ref(false)
const currentCode = ref('')
const currentLang = ref('')

function codeToMarkdown(code, programType) {
  let lang = 'java'
  if (programType === 1) lang = 'java'
  else if (programType === 2) lang = 'cpp'
  else if (programType === 3) lang = 'python'
  else lang = ''
  return `\`\`\`${lang}\n${code}\n\`\`\``
}

function showCode(row) {
  currentCode.value = row.userCode
  currentLang.value = row.programType
  dialogVisible.value = true
}

onMounted(async () => {
  if (questionId) {
    const courseId = route.query.courseId
    const examId = route.query.examId
    const res = await getHomeworkUserDTOListService(questionId, courseId, examId)
    if (res.code === 1000) {
      submissionList.value = res.data || []
    }
  }
})
</script> 